/**
 * 
 */
package org.mspring.mlog.service.search.impl;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextSession;
import org.mspring.mlog.service.search.BaseSearch;
import org.mspring.platform.persistence.hibernate.HibernateUtils;
import org.mspring.platform.persistence.support.Page;
import org.mspring.platform.utils.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Gao Youbo
 * @since 2013-1-9
 * @Description
 * @TODO
 */
@SuppressWarnings({ "rawtypes" })
@Service
@Transactional
public class BaseHibernateSearch<T> implements BaseSearch<T> {

    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private HibernateSearchTemplate hibernateSearchTemplate;

    private Class entityClass;

    public BaseHibernateSearch() {
        this.entityClass = ClassUtils.getSuperClassGenricType(getClass(), 0);
    }

    public BaseHibernateSearch(Class<T> entityClass, SessionFactory sessionFactory) {
        this.entityClass = ClassUtils.getSuperClassGenricType(getClass(), 0);
        this.sessionFactory = sessionFactory;
    }

    protected final Session getSession() {
        return HibernateUtils.getSession(sessionFactory);
    }

    protected final HibernateSearchTemplate getHibernateSearchTemplate() {
        return hibernateSearchTemplate;
    }

    @Override
    public FullTextSession getFullTextSession() {
        // TODO Auto-generated method stub
        return hibernateSearchTemplate.getFullTextSession();
    }

    @Override
    public void updateIndex(Serializable id) {
        // TODO Auto-generated method stub
        hibernateSearchTemplate.updateIndex(entityClass, id);
    }

    @Override
    public void updateAllIndex() {
        // TODO Auto-generated method stub
        hibernateSearchTemplate.updateAllIndex(entityClass);
    }

    @Override
    public void updateAllIndex(int batchSize) {
        // TODO Auto-generated method stub
        hibernateSearchTemplate.updateAllIndex(entityClass, batchSize);
    }

    @Override
    public Page listPage(Page page, String text, String... fields) {
        // TODO Auto-generated method stub
        return hibernateSearchTemplate.listPage(entityClass, page, text, fields);
    }

}
